/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Assignment.BLO;

import Assignment.DAO.BookDAO;
import Assignment.DTO.BookDTO;
import Assignment.DTO.BookListDTO;
import Assignment.Util.ConnectionUtil;
import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

/**
 *
 * @author dieptn
 */
public class BookBLO {

    private Connection con;

    public String getAllBooks() {
        String path = "";
        try {
            con = ConnectionUtil.getConnection();
            ArrayList<BookDTO> list = BookDAO.search(con, null);
            //path = "C:/books.xml";
            path = ConnectionUtil.getBookFilePath();
            if (list.size() > 0) {
                BookListDTO listBook = new BookListDTO();
                for (int i = 0; i < list.size(); i++) {
                    listBook.getBookList().add(list.get(i));
                }
                JAXBContext ctx = JAXBContext.newInstance(BookListDTO.class);
                Marshaller mar = ctx.createMarshaller();
                mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
                mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
                mar.marshal(listBook, new File(path));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return path;
    }

    public String getAllLastestBooks() {
        String path = "";
        try {
            con = ConnectionUtil.getConnection();
            ArrayList<BookDTO> list = BookDAO.getNewestBooks(con);
            //path = "C:/books.xml";
            path = ConnectionUtil.getNewBookFilePath();
            if (list.size() > 0) {
                BookListDTO listBook = new BookListDTO();
                for (int i = 0; i < list.size(); i++) {
                    listBook.getBookList().add(list.get(i));
                }
                JAXBContext ctx = JAXBContext.newInstance(BookListDTO.class);
                Marshaller mar = ctx.createMarshaller();
                mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
                mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
                mar.marshal(listBook, new File(path));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return path;
    }

    public boolean insertBook(BookDTO book) {
        con = ConnectionUtil.getConnection();
        if (BookDAO.insert(con, book) > 0) {
            return true;
        }
        return false;
    }

    public boolean updateBook(BookDTO book) {
        con = ConnectionUtil.getConnection();
        if (BookDAO.update(con, book) > 0) {
            return true;
        }
        return false;
    }

    public boolean deleteBook(String bookID) {
        con = ConnectionUtil.getConnection();
        if (BookDAO.delete(con, bookID) > 0) {
            return true;
        }
        try {
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(BookBLO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    public void deleteBooks(String[] bookIDs) {
        con = ConnectionUtil.getConnection();
        for (int i = 0; i < bookIDs.length; i++) {
            BookDAO.delete(con, bookIDs[i]);
        }
        try {
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(BookBLO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String generateBookID() {
        con = ConnectionUtil.getConnection();
        return BookDAO.generateID(con);
    }
}
